<?php
class Manufacturer_model extends CI_Model{
	function __construct(){
		parent::__construct();
	}

	public function get_all(){
		$query = $this->db->get('manufacturer');
		return $query->result();
	}

	public function get_updated($day){
		$this->db->where('manufacturer_modified_date >= DATE_SUB(NOW(), INTERVAL '.$day.' DAY)');
		$query = $this->db->get('manufacturer');
		return $query->result();
	}

	public function add($manufacturer_name, $active){
		$data = array(
				'manufacturer_name' => utf8_encode($manufacturer_name) ,
				'manufacturer_active' => $active
		);
		if ($this->db->insert('manufacturer', $data))
			return $this->db->insert_id();
		else
			return -1;
	}

	public function update($manufacturer_id, $manufacturer_name){
		$data = array(
				'manufacturer_name' => utf8_encode($manufacturer_name)
		);
		$this->db->where('manufacturer_id', $manufacturer_id);
		if ($this->db->update('manufacturer', $data))
			return true;
		else
			return false;
	}

	public function get_id($manufacturer_name){
		//$query = $this->db->get_where('manufacturer', array('manufacturer_name' => $manufacturer_name));
		$query = $this->db->where('LOWER(manufacturer_name)', strtolower(utf8_encode($manufacturer_name)))->get('manufacturer');
		if ($query->num_rows() > 0)
		{
			$row = $query->row();
			return $row->manufacturer_id;
		}else
			return -1;
	}

	public function get_manufacturer_by_id($id){
		$query = $this->db->where('manufacturer_id', $id)->get('manufacturer');

		if ($query->num_rows() > 0)
		{
			return $query->row();
		}else
			return null;
	}

	public function search($string, $limit, $start){
		//if (strlen($string)>0){
		$this->db->like('manufacturer_name', $string, 'after');
		$this->db->or_like('manufacturer_name', ' '.$string);
		$this->db->limit($limit, $start);
		$query = $this->db->get('manufacturer');
		if ($query->num_rows() > 0)
		{
			return $query->result();
		}
		//}
		return null;
	}

	public function count_search($string){
		//if (strlen($string)>0){
		$this->db->like('manufacturer_name', $string, 'after');
		$this->db->or_like('manufacturer_name', ' '.$string);
		$this->db->from('manufacturer');
		return $this->db->count_all_results();
	}
}